//
//  DetailViewController.m
//  Style
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#define ViewWidth(v)                        v.frame.size.width
#define ViewHeight(v)                       v.frame.size.height
#import "YLDetailViewController.h"
#import "YLDetailTableViewCell.h"
#import "AFNetWorkingW.h"
#import "YLDetail_model.h"
#import <MJRefresh.h>
#import "ArticleModel.h"
#import "YLHeadScrollViewController.h"
#import "ArticleTableView_model.h"
#import "ArticleTableView_model.h"
@interface YLDetailViewController () <UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate>
{
    MJRefreshHeader *_refreshHeader;
    MJRefreshFooter *_moreFooter;
    BOOL _needRefresh;
    BOOL _hasMore;
    NSInteger   page;
    
    NSMutableArray *_arrData;
}

@property (nonatomic, assign) NSInteger limit;
@property (nonatomic, assign) NSInteger start;
@property (nonatomic, assign) NSInteger nextPage;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) YLDetailTableViewCell *detailTableViewCell;
@property (nonatomic, retain) NSMutableDictionary *mDic;
@property (nonatomic, retain) NSMutableArray *mArr;
@property (assign, nonatomic) BOOL automaticallyHidden;
@property (nonatomic, assign) BOOL open;
@property (nonatomic, retain) UIActivityIndicatorView *activity;
@end

@implementation YLDetailViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.mArr = [NSMutableArray array];
        
    self.start = 0;
    self.limit = 10;

    self.title = self.article_model.name;
    
    
    [self dataHandle];
    [self tableViewsubViews];
    [self setupRefresh];
    self.activity = [[UIActivityIndicatorView alloc] init];
    self.activity.color = [UIColor blackColor];
    self.activity.frame = CGRectMake(0, 0, 50, 50);
    self.activity.center = self.view.center;
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    
    [self setExtraCellLineHidden:self.tableView];
    
}

/* 数据处理 */
- (void)dataHandle
{
    [AFNetWorkingW netWorkWithURL:@"http://api2.pianke.me/read/columns_detail" body:[NSString stringWithFormat:@"auth=&client=1&deviceid=BA920378-52B5-43F2-BE1A-5404120EDD7A&limit=%ld&sort=hot&start=(%ld - 10)&typeid=%@&version=3.0.6",  (long)self.limit, (long)self.start, self.article_model.type] resultBlock:^(id result) {
        self.mDic = result;
        
        NSDictionary *dic = [self.mDic objectForKey:@"data"];
        NSArray *arr = [dic objectForKey:@"list"];
        if ([arr count] == [self.mArr count]) {
            [self.tableView.footer noticeNoMoreData];
        } else
        {
            self.mArr = [NSMutableArray array];
            for (NSDictionary *dic1 in arr) {
                self.detail_model = [[YLDetail_model alloc] init];
                [self.detail_model setValuesForKeysWithDictionary:dic1];
                [self.detail_model setTypeId:[dic1 objectForKey:@"id"]];
                [self.mArr addObject:self.detail_model];
                [self.tableView.footer endRefreshing];
            }
            [self.tableView reloadData];
            [self.activity stopAnimating];
        }
    }];
    self.limit += 10;
    self.start = self.limit - 10;
}

#pragma mark ** tableView相关设置
/* tableView布局 */
- (void)tableViewsubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth(self.view), ViewHeight(self.view)) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[YLDetailTableViewCell class] forCellReuseIdentifier:@"reuse"];
}

/* 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.mArr count];
}

/* cell设置 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.detailTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    self.detail_model = [self.mArr objectAtIndex:indexPath.row];
    self.detailTableViewCell.detail_model = self.detail_model;
    return self.detailTableViewCell;
}

/* 行高 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ViewHeight(self.view) / 3.5;
}

/* cell选择 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLHeadScrollViewController *head = [[YLHeadScrollViewController alloc] init];
    head.detail_model = [self.mArr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:head animated:YES];
}

/* 分割线隐藏 */
- (void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}


#pragma mark ** 刷新
- (void)setupRefresh
{
    /* 上拉刷新 */
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)];
    [self.tableView.header beginRefreshing];
    /* 下拉加载数据 */
    
    self.tableView.footer = [MJRefreshAutoGifFooter  footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)];
    [self.tableView.footer beginRefreshing];
}

- (void)headerRefreshing
{
    [self.tableView reloadData];
//    [self dataHandle];
    [self.tableView.header endRefreshing];
}

- (void)footerRefreshing
{
    self.tableView.footer.automaticallyHidden = YES;
    [self dataHandle];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
